﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess;
using System.Data;

namespace AMS.DataAccess
{
    public class MailDAL
    {
        /// <summary>
        /// 创建发送邮件
        /// </summary>
        /// <param name="templateCode"></param>
        /// <param name="oldstring"></param>
        /// <param name="newstring"></param>
        /// <returns></returns>
        public int CreateMail(string templateCode, string mailto, string mailcc, string[] oldstring, string[] newstring)
        {
            SQLHelper sqlHelper = new SQLHelper();


            string szSQL = string.Format("SELECT template_content,subject FROM COM_MailTemplate where template_code = '{0}'", templateCode);

            DataTable dt = sqlHelper.ExecuteDataTable(szSQL, false, null);
            string strTemplate_content = "";
            string strSubect = "";
            if (dt.Rows.Count > 0)
            {
                strTemplate_content = dt.Rows[0]["template_content"].ToString();
                strSubect = dt.Rows[0]["subject"].ToString();
            }
            else
            {
                throw new Exception(string.Format("COM_MailTemplate表找不到template_code = '{0}'记录！", templateCode));
            }

            if (oldstring != null)
            {
                strTemplate_content = Replace(strTemplate_content, oldstring, newstring);
                strSubect = Replace(strSubect, oldstring, newstring);
            }

            if (dt.Rows.Count >= 0)
            {
                szSQL = string.Format(@"INSERT INTO COM_SendMail (mail_id,html_text,is_send,subject,mail_cc,mail_to,save_time,retry_times,status,attachment_file) VALUES 
                            ('{0}','{1}','{2}','{3}','{4}','{5}','{6}',{7},{8},'{9}')",
                               System.Guid.NewGuid().ToString(),
                               strTemplate_content,
                               "N",
                               strSubect,
                               mailto,
                               mailcc,
                               System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                               0,
                               0,
                               "");
                dt.Dispose();
                return sqlHelper.ExecuteCommand(szSQL, false, null);
            }
            else
            { return 0; }


        }

        /// <summary>
        /// 替换字符串
        /// </summary>
        /// <param name="str"></param>
        /// <param name="oldstring"></param>
        /// <param name="newstring"></param>
        /// <returns></returns>
        private string Replace(string str, string[] oldstring, string[] newstring)
        {
            for (int i = 0; i < oldstring.Length; i++)
            {
                str = str.Replace(oldstring[i], newstring[i]);
            }

            return str;
        }
    }
}
